import './assets/plugins/vue/vue@2.6.12.min.js'
import './assets/plugins/vue/vue-router@3.5.1.min.js'
import './assets/plugins/vant/vant@2.12.min.js'
import gyList from './data/gylist.js'

import routes from './routes.js'

const router = new VueRouter({
	routes,
})

Vue.$toast = vant.Toast;

new Vue({
	el: '#app',
	router,
	data: {
		mode: 'prod',
		userId: '1', // 登陆人id
		nickName: '管理员', // 登陆人名称
		privateKey: '',
		publicKey: '',
		gyList,
	},
	created() {
		// this.insertConfigJS();
	},
	mounted() {
		console.log(`./config/config.${this.mode}.js`)
		import(`./config/config.${this.mode}.js`).
		then(m => {
			axios.defaults.baseURL = m.baseUrlPath;
			this.publicKey = m.publicKey;
			this.getAccount();
		});
	},
	computed: {
		desKey() {
			return this.privateKey.slice(0,8);
		},
		iv() {
			let arr = [];
			for (let i = 0; i < this.desKey.length; i++) {
				arr.push(this.desKey.codePointAt(i)); // ASCII值
			}
			return arr;
		}
	},
	methods: {
		// 获取用户信息
		getAccount() {
			// alert('location.href: ' + location.href);
			let fakeTokenText = this.parseUrlParam(location.href)['token'];
			if(!fakeTokenText) {
				this.$toast('URL中未获取到token信息');
				return;
			}
			
			fakeTokenText = fakeTokenText.slice(0, fakeTokenText.indexOf('#/'));
				
			this.privateKey = $.md5($.md5(this.publicKey).toUpperCase()).toUpperCase();
			// alert('token加密值--------' + fakeTokenText)
			
			let resultArr = this.decryptByDES(fakeTokenText, this.desKey).split(' ');
			// alert(resultArr)
			let result = resultArr[resultArr.length - 1];
			// alert(result)
			// console.log('解密值--------' + result)
			// alert('解密值--------' + result)
			
			axios.get(`/sys/getLoginInfoApp/${result}`, {
			}).then(resp => {
				this.nickName = resp.data.nickName;
				this.userId = resp.data.id;
			});
			
		},
		insertConfigJS() {
			let node = document.createElement('script');
			node.src = `./config/config.${this.mode}.js`;
			document.head.appendChild(node);
		},
		decryptByDES(ciphertext, key) {
			let keyHex = CryptoJS.enc.Utf8.parse(key);
			let decrypted = CryptoJS.DES.decrypt({
					ciphertext: CryptoJS.enc.Hex.parse(ciphertext)
			}, keyHex, {
					mode: CryptoJS.mode.CBC, // 模式
					padding: CryptoJS.pad.Pkcs7,
					iv: CryptoJS.enc.Utf8.parse(this.iv),
			});
			let result_value = decrypted.toString(CryptoJS.enc.Utf8);
			return result_value;
		},
		parseUrlParam(url) {
			let res = {};
			url.split(/\?|&/).filter(v => v).forEach(v => {
				let keyvalue = v.split('=');
				res[keyvalue[0]] = keyvalue[1];
			});
			
			return res;
		},
			
	}
})


